<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">管理员管理</a>
        <a>
          <cite>权限信息</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
          <input class="layui-input"   placeholder="要添加的权限描述" name="cate_name">
          <button class="layui-btn"  onclick="addCate(this,this.id)" ><i class="layui-icon"></i>增加</button>
        </div>
      </div>

        <span class="x-right" style="line-height:40px">共有数据：<span id="total"></span> 条</span>

      <table class="layui-table">
        <thead>
          <tr>
            <th>权限代码</th>
            <th>权限描述</th>
        </thead>
        <tbody id="data-body">
          <tr>
            <td>1</td>
            <td>权限相关</td>

          </tr>
        </tbody>
      </table>


    </div>
    <script>

      layui.use('laydate', function(){
        var laydate = layui.laydate;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });

      function addCate(obj,id){

        layer.confirm('确认添加该权限？',function(index){
          //发异步删除数据

          let add=$("input[name='cate_name']").val();
          if(add!=""){
          $.ajax({
            type:"post",
            url:"/access/addnew",
            data: {"permission":add},
            dataType:"json",
            success:function (res) {
              if(res.code === 200){
                layer.msg('添加成功!',{icon:1,time:1000});
                getPageData(0, 0);

              }
            }
          })}
        });

      }




      /*查询数据的方法*/
      function getPageData(page, pageSize){
        // 获取数据列表
        $.ajax({
          type:"post",
          url:"/access/cate/list",
          data:null,
          dataType:"json",

          success:function (data) {
            console.log(data);
            if(data.code === 200){
              //总条数
              $("#total").text(data.data.length);
              // 清空数据
              $("#data-body").empty();
              // 遍历数据的数组,生成对应的行


              $(data.data).each(function () {

                // 创建数据行的dom对象
                let tr = $('<tr>\n' +
                        '            <td>'+this.permissionCode+'</td>\n' +
                        '            <td>'+this.permissionDisplay+'</td>\n' +
                        '              </a>\n' +
                        '            </td>\n' +
                        '          </tr>');
                // 将数据追加到 tbody中
                $("#data-body").append(tr);

              });

            }

          }
        });
      }

      /*让整个dom文档加载完成之后再执行*/
      $(function () {
        getPageData(0, 0);
      })
    </script>

  </body>

</html>